class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if (head.val == val) return head.next;
        ListNode node = head, front = null;
        while (node != null) {
            if (node.val == val) {
                front.next = node.next;
                node.next = null;
            }
            front = node;
            node = node.next;
        }
        return head;
    }
}
